package com.ruoyi.sale.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.base.domain.GasBalance;
import com.ruoyi.base.mapper.GasBalanceMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sale.mapper.SaleRecordMapper;
import com.ruoyi.sale.domain.SaleRecord;
import com.ruoyi.sale.service.ISaleRecordService;

/**
 * 销售记录
Service业务层处理
 *
 * @author h w
 * @date 2022-12-09
 */
@Service
public class SaleRecordServiceImpl implements ISaleRecordService
{
    @Autowired
    private SaleRecordMapper saleRecordMapper;
    @Autowired
    private GasBalanceMapper gasBalanceMapper;

    /**
     * 查询销售记录

     *
     * @param saleRecordId 销售记录
主键
     * @return 销售记录

     */
    @Override
    public SaleRecord selectSaleRecordBySaleRecordId(Long saleRecordId)
    {
        return saleRecordMapper.selectSaleRecordBySaleRecordId(saleRecordId);
    }

    /**
     * 查询销售记录
列表
     *
     * @param saleRecord 销售记录

     * @return 销售记录

     */
    @Override
    public List<SaleRecord> selectSaleRecordList(SaleRecord saleRecord)
    {
        return saleRecordMapper.selectSaleRecordList(saleRecord);
    }

    /**
     * 新增销售记录

     *
     * @param saleRecord 销售记录

     * @return 结果
     */
    @Override
    public int insertSaleRecord(SaleRecord saleRecord)
    {
        int res = 0;
        GasBalance balance = new GasBalance();
        balance.setEnterpriseId(saleRecord.getEnterpriseId());
        List<GasBalance> balanceSupplier = gasBalanceMapper.selectGasBalanceList(balance);
        if(balanceSupplier != null && balanceSupplier.size() > 0) {
            GasBalance balanceModify = balanceSupplier.get(0);
            BigDecimal rest = balanceModify.getCustomerBalance().subtract(saleRecord.getSalePrice().multiply(saleRecord.getFactAmount()));
            if(rest.compareTo(BigDecimal.ZERO) > 0) {
                balanceModify.setCustomerBalance(rest);
                balanceModify.setUpdateTime(DateUtils.getNowDate());
                balanceModify.setUpdateBy(saleRecord.getCreateBy());
                res = gasBalanceMapper.updateGasBalance(balanceModify);
            }
        }
        balance.setEnterpriseId(null);
        balance.setCustomerId(saleRecord.getCustomerId());
        List<GasBalance> balanceList = gasBalanceMapper.selectGasBalanceList(balance);
        if(balanceList != null && balanceList.size() > 0) {
            GasBalance balanceModify = balanceList.get(0);
            BigDecimal rest = balanceModify.getCustomerBalance().subtract(saleRecord.getOutAmount().multiply(saleRecord.getOutPrice()));
            if(rest.compareTo(BigDecimal.ZERO) > 0) {
                balanceModify.setCustomerBalance(rest);
                balanceModify.setUpdateTime(DateUtils.getNowDate());
                balanceModify.setUpdateBy(saleRecord.getCreateBy());
                res += gasBalanceMapper.updateGasBalance(balanceModify);
            }
        }
        if(res == 2) {
            saleRecord.setCreateTime(DateUtils.getNowDate());
            return saleRecordMapper.insertSaleRecord(saleRecord);
        }
        return 0;
    }

    /**
     * 修改销售记录

     *
     * @param saleRecord 销售记录

     * @return 结果
     */
    @Override
    public int updateSaleRecord(SaleRecord saleRecord)
    {
        saleRecord.setUpdateTime(DateUtils.getNowDate());
        return saleRecordMapper.updateSaleRecord(saleRecord);
    }

    /**
     * 批量删除销售记录

     *
     * @param saleRecordIds 需要删除的销售记录
主键
     * @return 结果
     */
    @Override
    public int deleteSaleRecordBySaleRecordIds(Long[] saleRecordIds)
    {
        return saleRecordMapper.deleteSaleRecordBySaleRecordIds(saleRecordIds);
    }

    /**
     * 删除销售记录
信息
     *
     * @param saleRecordId 销售记录
主键
     * @return 结果
     */
    @Override
    public int deleteSaleRecordBySaleRecordId(Long saleRecordId)
    {
        return saleRecordMapper.deleteSaleRecordBySaleRecordId(saleRecordId);
    }
}
